package com.web.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Code;
import com.common.entity.Pile;
import com.common.entity.ResponseResult;
import com.common.entity.Site;
import com.web.main.entity.form.PileForm;
import com.web.main.entity.param.PileQuery;
import com.web.main.entity.vo.PileVo;
import com.web.main.service.PileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 长顺
 * @since 2020-11-21
 */
//@CrossOrigin
@Api(tags = "电桩接口")
@RestController
@RequestMapping("/pile")
@Validated
@CrossOrigin
public class PileController {
    @Autowired
    PileService pileService;


    //测试接口
    @CrossOrigin
    @GetMapping("/getPile")
    public List<Site> getPiles() {
        Site site = new Site();
        site.setSiteLongitude("104.065861");
        site.setSiteDimensionality("30.657401");
        Site site1 = new Site();
        site1.setSiteLongitude("104.063402");
        site1.setSiteDimensionality("30.568744");
        ArrayList<Site> objects = new ArrayList<>();
        objects.add(site);
        objects.add(site1);
        return objects;
    }

    @ApiOperation(value = "添加电桩")
    @PostMapping("/add")
    public ResponseResult add(@ApiParam(value = "电桩对象")
                              @Validated(PileForm.Add.class) PileForm pileForm) {
        pileService.addPile(pileForm);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "删除电桩")
    @DeleteMapping("/delete")
    public ResponseResult deleteOne(@ApiParam(value = "电桩主键id", required = true)
                                    @RequestParam("pileId") String pileId) {
        pileService.removePileById(pileId);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "修改电桩")
    @PutMapping("/modify")
    public ResponseResult modify(@ApiParam(value = "电桩对象")
                                 @Validated(PileForm.Modify.class) PileForm pileForm) {
        pileService.modifyPile(pileForm);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "获取一个电桩信息")
    @GetMapping("/getOne")
    public ResponseResult getOne(@ApiParam(value = "电桩主键id", required = true)
                                 @RequestParam String pileId) {
        PileVo pileVo = pileService.getPileById(pileId);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("pile", pileVo);
    }


    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public ResponseResult pageQuery(@ApiParam(value = "条件对象")
                                    @RequestBody PileQuery pileQuery) {
        Page<PileVo> pileVoPage = pileService.pageQuery(pileQuery);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("pilePage", pileVoPage);
    }

    @ApiOperation(value = "获取电桩个数")
    @GetMapping("/getCount")
    public ResponseResult getCount() {
        QueryWrapper<Pile> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("pile_state", -1);
        int count = pileService.count(queryWrapper);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("getCount", count);
    }
}

